package com.zs.leymovie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;
import com.zs.leymovie.bean.Seat;
import com.zs.leymovie.bean.SeatData;
import com.zs.leymovie.bean.SeatInfo;
import com.zs.leymovie.constants.UrlConstants;
import com.zs.leymovie.listener.OnSeatClickListener;
import com.zs.leymovie.view.SSThumView;
import com.zs.leymovie.view.SSView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@ContentView(R.layout.activity_xuan_zuo)
public class XuanZuoActivity extends AppCompatActivity {
    private int ROW = 30;
    private int EACH_ROW_COUNT = 30;
    private SSView mSSView;
    private SSThumView mSSThumView;
    private ArrayList<SeatInfo> list_seatInfos = new ArrayList<SeatInfo>();
    private ArrayList<ArrayList<Integer>> list_seat_conditions = new ArrayList<ArrayList<Integer>>();
    private ArrayList<SeatData> seatDatas = new ArrayList<SeatData>();
    private String city_id;
    private String show_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        city_id = intent.getStringExtra(UrlConstants.CITY_ID);
        show_id = intent.getStringExtra(UrlConstants.SHOW_ID);
        init();
        initData();

    }

    private void initData() {
        String url = UrlConstants.getXuanZUO(city_id, show_id);
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String info = responseInfo.result;
                try {
                    JSONObject object = new JSONObject(info);
                    JSONObject data = object.getJSONObject("data");
                    JSONObject play_data = data.getJSONObject("play_data");
                    JSONArray seat_data = play_data.getJSONArray("seat_data");
                    if (seat_data != null && seat_data.length() > 0) {
                        for (int i = 0; i < seat_data.length(); i++) {
                            JSONArray seat = seat_data.getJSONArray(i);
                            String num = seat.getString(0);
                            String flag = seat.getString(1);
                            String sell = seat.getString(2);
                            String row = seat.getString(3);
                            String cow = seat.getString(4);
                            String desc = seat.getString(5);
                            SeatData seatData = new SeatData(num, flag, sell, row, cow, desc);
                            seatDatas.add(seatData);
                        }
                    }else{
                        Toast.makeText(XuanZuoActivity.this,"本场次占时未提供选座",Toast.LENGTH_LONG).show();
                    }
                    String max_x = play_data.getString("max_x");
                    String max_y = play_data.getString("max_y");
                    if (max_x != null && !TextUtils.equals(max_x, "") && max_x.length() > 0) {
                        ROW = Integer.parseInt(max_x);
                        EACH_ROW_COUNT = Integer.parseInt(max_y);
                    } else {
                        ROW = 30;
                        EACH_ROW_COUNT = 30;
                        Toast.makeText(XuanZuoActivity.this, "本场次座位票已经售完或者停止出售", Toast.LENGTH_LONG).show();
                    }
                    setSeatInfo();
                    mSSView.init(EACH_ROW_COUNT, ROW, list_seatInfos, list_seat_conditions, mSSThumView, 5);
                    String cinema_name = play_data.getString("cinema_name");
                    detail_title.setText(cinema_name);
                    String movie_name = play_data.getString("movie_name");
                    detail_movename.setText(movie_name);

                    String date = play_data.getString("play_date");
                    String week = null;
                    try {
                        week = getFullDateWeekTime(getDate(date).getTime());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    String start_time = play_data.getString("start_time");
                    detail_data.setText(date + "   " + week + "  " + start_time);
                    String hall_name = play_data.getString("hall_name");
                    tv_cinema_screen.setText(hall_name);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                UrlConstants.getPrompt();
            }
        });
    }

    private void init() {

        mSSView = (SSView) this.findViewById(R.id.mSSView);
        mSSThumView = (SSThumView) this.findViewById(R.id.ss_ssthumview);
//		mSSView.setXOffset(20);
        mSSView.setOnSeatClickListener(new OnSeatClickListener() {

            @Override
            public boolean b(int column_num, int row_num, boolean paramBoolean) {
                /*String desc = "第" + (row_num + 1) + "排" + (column_num + 1) + "号";
                Toast.makeText(XuanZuoActivity.this, desc.toString(), Toast.LENGTH_SHORT).show();*/

                for (int i = 0; i < seatDatas.size(); i++) {
                    SeatData seatData = seatDatas.get(i);
                    int row = Integer.parseInt(seatData.getRow());
                    int cow = Integer.parseInt(seatData.getCow());
                    if (column_num == (cow-1) && (row-1) == row_num) {
                        String dese = seatData.getDesc();
                        Toast.makeText(XuanZuoActivity.this, dese, Toast.LENGTH_LONG).show();
                        return false;
                    }
                }
                return false;
            }

            @Override
            public boolean a(int column_num, int row_num, boolean paramBoolean) {
                for (int i = 0; i < seatDatas.size(); i++) {
                    SeatData seatData = seatDatas.get(i);
                    int row = Integer.parseInt(seatData.getRow());
                    int cow = Integer.parseInt(seatData.getCow());
                    if (column_num == (cow-1) && (row-1) == row_num) {
                        String dese = seatData.getDesc();
                        Toast.makeText(XuanZuoActivity.this, dese, Toast.LENGTH_LONG).show();
                        return false;
                    }
                }
                return false;
            }

            @Override
            public void a() {
                // TODO Auto-generated method stub

            }
        });

        xz_item_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void setSeatInfo() {
        for (int i = 0; i < ROW; i++) {
            SeatInfo mSeatInfo = new SeatInfo();
            ArrayList<Seat> mSeatList = new ArrayList<Seat>();
            //记录作为的状态，0位过道，2为已选，1为可选
            ArrayList<Integer> mConditionList = new ArrayList<Integer>();
            for (int j = 0; j < EACH_ROW_COUNT; j++) {
                Seat mSeat = new Seat();
                mSeat.setN("Z");
                mConditionList.add(0);
                /*if (j <= 0) {
                    mSeat.setN("Z");
                    mConditionList.add(0);
                } else {
                    //mSeat.setN(String.valueOf(j - 2));
                    if (j > 10) {
                        mConditionList.add(2);
                    } else {
                        mConditionList.add(1);
                    }

                }*/
                /*if (seatDatas != null && seatDatas.size() > 0) {
                    for (int k = 0; k < seatDatas.size(); k++) {
                        SeatData seatData = seatDatas.get(k);
                        int row = Integer.parseInt(seatData.getRow());
                        int cow = Integer.parseInt(seatData.getCow());
                        if (row <= i && cow <= j) {
                            mSeat.setN(String.valueOf(k));
                            if (Integer.parseInt(seatData.getSell()) == 0) {
                                mConditionList.add(1);
                            } else {
                                mConditionList.add(2);
                            }
                            break;
                        }else {
                            mSeat.setN("Z");
                            mConditionList.add(0);
                        }
                    }
                }*/
                mSeat.setDamagedFlg("");
                mSeat.setLoveInd("0");
                mSeatList.add(mSeat);
            }
            mSeatInfo.setDesc(String.valueOf(i + 1));
            mSeatInfo.setRow(String.valueOf(i + 1));
            mSeatInfo.setSeatList(mSeatList);
            list_seatInfos.add(mSeatInfo);
            list_seat_conditions.add(mConditionList);
        }
        for (int i = 0; i < seatDatas.size(); i++) {
            SeatData seatData = seatDatas.get(i);
            int row = Integer.parseInt(seatData.getRow());
            int cow = Integer.parseInt(seatData.getCow());
            list_seatInfos.get(row - 1).getSeat(cow - 1).setN("1");
            if (Integer.parseInt(seatData.getSell()) == 0) {
                list_seat_conditions.get(row - 1).set(cow - 1, 1);
            } else {
                list_seat_conditions.get(row - 1).set(cow - 1, 2);
            }

        }


    }

    public static String getFullDateWeekTime(long date) {
        try {
            String formater = "yyyy-MM-dd";
            SimpleDateFormat format = new SimpleDateFormat(formater);
            format.applyPattern("E");
            return format.format(date);
        } catch (Exception ex) {
            System.out
                    .println("TimeUtil getFullDateWeekTime" + ex.getMessage());
            return "";
        }

    }

    public static Date getDate(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(strDate);
        return date;
    }

    @ViewInject(R.id.xz_item_back)
    private ImageView xz_item_back;


    @ViewInject(R.id.detail_title)
    private TextView detail_title;

    @ViewInject(R.id.detail_movename)
    private TextView detail_movename;

    @ViewInject(R.id.detail_data)
    private TextView detail_data;

    @ViewInject(R.id.detail_select)
    private TextView detail_select;

    @ViewInject(R.id.tv_cinema_screen)
    private TextView tv_cinema_screen;


    @ViewInject(R.id.detail_yixuan)
    private TextView detail_yixuan;

    @ViewInject(R.id.detail_yixiou)
    private TextView detail_yixiou;

    @ViewInject(R.id.detail_qinglv)
    private TextView detail_qinglv;


    @ViewInject(R.id.detail_zhendong)
    private TextView detail_zhendong;


}

